package com.share.hope.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.share.hope.entity.PoorChildren;
import com.share.hope.entity.UserCollectPoorChild;
import com.share.hope.entity.page.Condition;
import com.share.hope.entity.page.PageQueryVo;
import com.share.hope.entity.result.PageR;
import com.share.hope.entity.result.R;
import com.share.hope.mapper.PoorChildrenMapper;
import com.share.hope.service.PoorChildCollectService;
import com.share.hope.service.PoorChildrenService;
import com.share.hope.vo.PoorChildPageQueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PoorChildrenServiceImpl extends BaseServiceImpl<PoorChildrenMapper, PoorChildren> implements PoorChildrenService {

    private final PoorChildCollectService poorChildCollectService;

    @Override
    public R<String> savePoorChildren(PoorChildren poorChildren) {
        if (this.save(poorChildren)) {
            return R.ok("save success");
        }
        return R.error("save fail");
    }

    @Override
    public R<String> updatePoorChildren(PoorChildren poorChildren) {
        if (this.updateById(poorChildren)) {
            return R.ok("update success");
        }
        return R.error("update fail");
    }

    @Override
    public R<String> deletePoorChildren(Long id) {
        if (this.removeById(id)) {
            return R.ok("delete success");
        }
        return R.error("delete fail");
    }

    @Override
    public R<PageR<PoorChildren>> pagePoorChildren(PageQueryVo<PoorChildPageQueryVo> queryVo) {
        PoorChildPageQueryVo query = queryVo.getQuery();

        LambdaQueryWrapper<PoorChildren> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getName()), PoorChildren::getName, query.getName());
        wrapper.eq(!ObjectUtils.isEmpty(query.getAge()), PoorChildren::getAge, query.getAge());
        wrapper.eq(!ObjectUtils.isEmpty(query.getGender()), PoorChildren::getGender, query.getGender());
        wrapper.like(StringUtils.hasText(query.getAddress()), PoorChildren::getAddress, query.getAddress());
        wrapper.eq(!ObjectUtils.isEmpty(query.getIsEnrolled()), PoorChildren::getIsEnrolled, query.getIsEnrolled());

        return R.success(new PageR<>(this.page(Condition.getPage(queryVo.getPage()), wrapper)));
    }

    @Override
    public R<PoorChildren> getPoorChildrenById(Long id) {
        return R.success(this.getById(id));
    }

    @Override
    public R<List<PoorChildren>> getAllPoorChildren(Long userId) {
        if(ObjectUtils.isEmpty(userId)){
            return R.success(this.list());
        }

        LambdaQueryWrapper<UserCollectPoorChild> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(userId), UserCollectPoorChild::getUserId, userId);
        List<UserCollectPoorChild> lists = poorChildCollectService.list(wrapper);

        List<Long> childIds;
        LambdaQueryWrapper<PoorChildren> poorChildrenLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!CollectionUtils.isEmpty(lists)) {
            childIds = lists.stream().map(UserCollectPoorChild::getChildId).collect(Collectors.toList());
            poorChildrenLambdaQueryWrapper.in(PoorChildren::getId, childIds);
        }

        return R.success(this.list(poorChildrenLambdaQueryWrapper));

    }
}
